package main

import (
	//"fmt"
	"net/http"
	"flag"
	//"io/ioutil"
	//"path"
	//"os"
)

/*
type Hello struct {
	dir string
}

func (h Hello) ListDirectory(w http.ResponseWriter, r *http.Request){

	fmt.Fprint(w, "<html><body>")

	files, _ := ioutil.ReadDir(path.Join(h.dir, r.URL.Path))
	for _, f := range files {
		fmt.Fprint(w, "<a href='" + path.Join(r.URL.Path, f.Name()) + "'>" + f.Name() + "</a>\n")
	}

	fmt.Fprint(w, "</body></html>")
}

func (h Hello) ServeFile(w http.ResponseWriter, r *http.Request){
	http.FileServer(http.Dir(path.Join(h.dir, r.URL.Path)))
}

func (h Hello) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request) {
	
	file, err := os.Open(path.Join(h.dir, r.URL.Path))
	if err != nil {
		fmt.Fprint(w, err)
		return
	}
	defer file.Close()
	fi, err := file.Stat()
	if err != nil {
		fmt.Fprint(w, err)
		return
	}
	switch mode := fi.Mode(); {
	case mode.IsDir():
		h.ListDirectory(w, r)
	case mode.IsRegular():
		h.ServeFile(w, r)
	}
}
*/
func main(){
	dirPtr := flag.String("dir", "", "directory to serve")

	flag.Parse()

	//h := Hello { *dirPtr }
	http.ListenAndServe("0.0.0.0:4000", http.FileServer(http.Dir(*dirPtr)))

	//http.ListenAndServe("0.0.0.0:4000", h)
}
